package com.xiaomi.service.impl;

import com.xiaomi.dao.CartDao;
import com.xiaomi.dao.ProductDao;
import com.xiaomi.dao.impl.CartDaoImpl;
import com.xiaomi.dao.impl.ProductDaoImpl;
import com.xiaomi.entity.Cart;
import com.xiaomi.entity.Product;
import com.xiaomi.service.CartService;

import java.math.BigDecimal;
import java.util.List;

/**
 * TODO
 *
 * @author jy
 * @date 2021/5/24
 */
public class CartServiceImpl implements CartService {

    private final CartDao cartDao = new CartDaoImpl();
    private final ProductDao productDao = new ProductDaoImpl();

    @Override
    public int createCart(String pid, int uid) {
        try {
            if (pid != null && !"".equals(pid)) {
                int p_id = Integer.parseInt(pid);
                //添加购物车前查询购物车是否存在这个用户添加的商品
                Cart cart = cartDao.hasCart(p_id, uid);
                if (cart != null) {
                    //如果cart不为null,则说明该用户已经添加过该商品进购物车
                    //及时更新商品数量
                    cart.setNum(cart.getNum() + 1);
                    return cartDao.update(cart);
                } else {
                    //如果cart为null,说明第一次加入购物车
                    //需要添加新的购物车
                    cart = new Cart(-1, p_id, uid, 1, null, null);
                    Product product = productDao.getProductByPid(p_id);
                    cart.setMoney(product.getPrice());
                    cart.setProduct(product);
                    return cartDao.insertCart(cart);
                }
            } else {
                return -1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public List<Cart> showCartsByUid(int uid) {
        try {
            return cartDao.showCartsByUid(uid);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int update(String cid, String num, String price) {
        if (cid != null && !"".equals(cid) && num != null &&
                !"".equals(num) && price != null && !"".equals(price)) {
            try {
                //num * price计算出money
                BigDecimal money = new BigDecimal(price);
                money = money.multiply(new BigDecimal(num));
                return cartDao.update(Integer.parseInt(cid), Integer.parseInt(num), money);
            } catch (Exception e) {
                e.printStackTrace();
                return -1;
            }
        }
        return -1;
    }

    @Override
    public int delete(String cid) {
        if (cid != null && !"".equals(cid)) {
            try {
                return cartDao.delete(Integer.parseInt(cid));
            } catch (Exception e) {
                e.printStackTrace();
                return -1;
            }
        }
        return -1;
    }

    @Override
    public int clear(String uid) {
        if (uid != null && !"".equals(uid)) {
            try {
                return cartDao.clear(Integer.parseInt(uid));
            } catch (Exception e) {
                e.printStackTrace();
                return -1;
            }
        }
        return -1;
    }

}
